package com.example.blog.util;


import com.example.blog.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.time.Duration;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Classname RedisCacheUtil
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/2/13 13:12
 * @Created by wlh12
 */
@Repository
public class RedisCacheUtil {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * string类型的存入缓存（需要设置过期时间）
     * @param key
     * @param value
     * @param expireTime 设置过期时间（分钟数）
     */
    public void set(String key,Object value,Long expireTime){
        try {
            if(expireTime>0){
                redisTemplate.opsForValue().set(key,value, Duration.ofMinutes(expireTime));
            } else {
                redisTemplate.opsForValue().set(key,value);
            }
        } catch (Exception e){
            throw new BusinessException("Redis缓存异常");
        }
    }
    /**
     * string类型的存入缓存（不设置过期时间）
     * @param key
     * @param value
     */
    public void set(String key,Object value){
        try {
            redisTemplate.opsForValue().set(key,value);
        } catch (Exception e){
            throw new BusinessException("Redis缓存异常");
        }
    }
    /**
     * 设置某一个key的过期时长
     * @param key
     * @param expireTime 分钟数
     */
    public void expire(String key,Long expireTime){
        try {
            redisTemplate.expire(key,Duration.ofMinutes(expireTime));
        } catch (Exception e){
            throw new BusinessException("Redis缓存异常");
        }
    }

    /**
     * string-object的get
     * @param key
     * @return
     */
    public Object get(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e){
            throw new BusinessException("Redis缓存异常");
        }
    }
    /**
     * 删除指定的key （一个或多个）
     * @param key
     */
    public void del(String... key){
        try {
            if (key!=null){
                if (key.length==1){
                    redisTemplate.delete(key[0]);
                } else {
                    redisTemplate.delete(Arrays.stream(key).collect(Collectors.toList()));
                }
            }
        } catch (Exception e){
            throw new BusinessException("Redis缓存异常");
        }
    }

    /**
     * 判断某一个key是否存在
     * @param key
     * @return
     */
    public Boolean hasKey(String key){
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            throw new BusinessException("Redis缓存异常");
        }
    }

    public long getExpireTime(String key){
        try {
            return redisTemplate.getExpire(key);
        } catch (Exception e){
            throw new BusinessException("Redis缓存异常");
        }
    }
}
